<?php

/**
 * @name: HOTN AnimeList
 * @author: HammerOnTheNail
 * @link: http://code.google.com/p/hotnanimelist/
 */

include("../../class2.php");
include(e_PLUGIN."hotn_animelist/Languages/".e_LANGUAGE.".php");
include(e_PLUGIN."hotn_animelist/Class/animelist_mysql.php");
include(e_PLUGIN."hotn_animelist/Class/animelist_options.php");

if(is_numeric(e_QUERY))
{
	// Letöltés végrehajtása - eredeti e107-es download alapján
	
	// Paraméter megadva, ami egy szám
	$id = (int)e_QUERY;
	
	// Adatok lekérése
	$sql->db_Select("hotn_animelist_download","*","`download_id` = '".$id."'");
	$download = $sql->db_Fetch();
	
	$sql->db_Select("hotn_animelist_project","*","`project_id` = '".$download['download_project']."'");
	$project = $sql->db_Fetch();
	
	if($HOTN_ANIMELIST_OPTIONS['download_'.$download['download_type']] <> 0)
	{
		if($HOTN_ANIMELIST_OPTIONS['download_'.$download['download_type']] == e_UC_MEMBER)
		{
			if (!USER)
			{
				require_once(HEADERF);
				$ns -> tablerender(HOTN_ANIMELIST_DEFAULT_ERROR, HOTN_ANIMELIST_DOWNLOAD_ERROR_NOT_LOGEDIN, "");
				require_once(FOOTERF);
				die();
			}
		}
	}
	
	// file teljes elérési útvonallal - link
	$file = $HOTN_ANIMELIST_OPTIONS['download_url']."/".$project['project_folder']."/".$download['download_type']."/".$download['download_url'];
	// file teljes elérési útvonallal - path
	$filepath = e_DOWNLOAD.$project['project_folder']."/".$download['download_type']."/".$download['download_url'];
	// file alap neve
	$filename = basename($download['download_url']);
	// file méret
	$filesize = filesize($filepath);
	
	//$file = "http://uh.x8.hu/letoltes/download/aki_sora/anime/%5BUraharaShop%5D%20Aki%20Sora%20-%2001%20OVA%20%28DVD%20720x480%20h264%20AAC%29.mkv";
	
	
	$hdrs = get_headers($file);
	
	print "<pre>";
	print_r($hdrs);
	print "</pre>";
	
	if ( (is_array($hdrs) ? preg_match('/^HTTP\\/\\d+\\.\\d+\\s+(404|510)\\s+.*$/',$hdrs[0]) : true) )
	{
		$file = SITEURL.$DOWNLOADS_DIRECTORY.$project['project_folder']."/".$download['download_type']."/".$download['download_url'];
	}
	
	print $file;
	
	die();

	
	if (!is_file($filepath))
	{
		
		// Automatikus hiba bejelentés
		$add = array(
			"error_title" => "HOTN AnimeList Download - ".$filename,
			"error_text" => "NotFound: ".$filename."<br/>URL: ".$file."<br/>PATH: ".$filepath,
			"error_url" => e_SELF."?".e_QUERY,
			"error_datestamp" => time()
		);
		$sql->db_Insert("hotn_animelist_error",$add);
		
		// hiba üzenet megjelenítése
		require_once(HEADERF);
		$ns -> tablerender(HOTN_ANIMELIST_DEFAULT_ERROR, HOTN_ANIMELIST_DOWNLOAD_ERROR_NO_FILE.$filename."<br>".HOTN_ANIMELIST_DOWNLOAD_DEFAULT_AUTO_ERROR_SEND, "");
		require_once(FOOTERF);
	}
	else
	{
		// Letöltési kérelmek frissításe
		$sql->db_Update("hotn_animelist_download","`download_request` = `download_request` + 1 WHERE `download_id` = '".$id."'");

		//print $file;
		
		//die();
		
		header("Location: ".$file);
	
		/*
	    //Begin writing headers
	    header("Pragma: public");
	    header("Expires: 0");
	    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
	    header("Cache-Control: public");
	    header("Content-Description: File Transfer");
	   
	    //Use the switch-generated Content-Type
	    header("Content-Type: application/force-download");
	
	    //Force the download
	    header("Content-Disposition: attachment; filename=".$filename.";");
	    header("Content-Transfer-Encoding: binary");
	    header("Content-Length: ".$filesize);
	    	
		/*
		
		header("Expires: 0");
		header("Cache-Control: max-age=30" );
		header("Content-Type: application/force-download");
		header("Content-Disposition: attachment; filename=\"{$filename}\"");
		header("Content-Length: {$filesize}");
		header("Pragma: public");
		if ($seek)
		{
			header("Accept-Ranges: bytes");
			header("HTTP/1.0 206 Partial Content");
			header("status: 206 Partial Content");
			header("Content-Range: bytes {$seek}-".($filesize - 1)."/{$filesize}");
		}
		
		die();
		
		while (!connection_aborted() && $filesize > 0)
		{
			echo fread($res , $bufsize);
			$filesize -= $bufsize;
		}
		fclose($res);
		
		/*
		// Fejléc beállítása
		header("Expires: 0");
		header("Cache-Control: max-age=30" );
		header("Content-Type: application/force-download");
		header("Content-Disposition: attachment; filename=\"{$filename}\"");
		header("Content-Length: {$filesize}");
		header("Pragma: public");
		
		// File "kiírása"
		$bufsize = 2048;
		$res =& fopen($filepath, 'rb');
		while (!connection_aborted() && $filesize > 0)
		{
			echo fread($res , $bufsize);
			$filesize -= $bufsize;
		}
		fclose($res);	
		*/
	}
}
else
{
	// Nincsen paraméter megadva
	header("Location: ".e_PLUGIN."hotn_animelist/project_list.php");
}
?>